package org.csu.hospital.controller;

import org.csu.hospital.domain.SignOn;
import org.csu.hospital.service.accountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class login {
    @Autowired
    private accountService accountService;

    //初始页面
    @RequestMapping("/")
    public String welcomeHtml()
    {
        return "login/login";
    }

//    @PostMapping("/phoneLogin")
//    @ResponseBody
//    public String phoneLogin()
//    {
//        return "0";
//    }

    //登录页面
    @PostMapping("/loginCheck")
    @ResponseBody
    public String loginCheck(HttpSession session,String username, String password)
    {
        SignOn signOn=new SignOn();
        signOn.setUsername(username);
        signOn.setPassword(password);

        System.out.println(username+password+"!!!!!!!!!!!!!!!!!!!");
        signOn = accountService.getAccountByUsernameAndPassword(signOn);
        if(signOn==null)
            return "0";
        else {
            session.setAttribute("signOn",signOn);
            return "1";
        }


    }
    @GetMapping("/login")
    public String loginIn(HttpSession session, Model model)
    {
        SignOn signOn =(SignOn) session.getAttribute("signOn");
        model.addAttribute("username",signOn.getUsername());
        model.addAttribute("password",signOn.getPassword());
        return "login/doctor";
    }
}
